﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;

namespace CoolERP_Web.User
{
    public partial class ZL_CP_Query : BasePage
    {
        protected string hxcbh
        {
            get { return this.GetUrlParam("hxcbh") == null ? "" : this.GetUrlParam("hxcbh").ToString(); }
        }
        protected string cpxbh
        {
            get { return this.GetUrlParam("cpxbh") == null ? "" : this.GetUrlParam("cpxbh").ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(hxcbh) || !string.IsNullOrEmpty(cpxbh))
                {
                    this.panQuery.Visible = false;
                    this.btnAdd.Visible = false;
                    this.btnDel.Visible = false;
                    this.GridView1.Columns[10].Visible = false;
                }
                initValue();
                this.AspNetPager1.PageSize = GridView1.PageSize;
                BindGridview();
            }
        }

        private void initValue()
        {
            BindCPX(this.ddlCPXBH, "请选择");
            if (!string.IsNullOrEmpty(cpxbh))
            {
                if (this.ddlCPXBH.Items.FindByValue(cpxbh) != null)
                    this.ddlCPXBH.Items.FindByValue(cpxbh).Selected = true;
                SelCPX();
            }
            if (!string.IsNullOrEmpty(hxcbh))
            {
                if (this.ddlCPMCBH.Items.FindByValue(hxcbh) != null)
                    this.ddlCPMCBH.SelectedValue = hxcbh;
            }

            BindCPLX(this.ddlCPLXBH, "请选择");
        }

        private void BindGridview()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(this.tbBH.Text))
                dc.Add("BH", this.tbBH.Text);
            if (!string.IsNullOrEmpty(ddlCPXBH.SelectedValue))
                dc.Add("CPXBH", ddlCPXBH.SelectedValue);
            if (!string.IsNullOrEmpty(ddlCPLXBH.SelectedValue))
                dc.Add("CPLX", ddlCPLXBH.SelectedValue);
            if (!string.IsNullOrEmpty(ddlCPMCBH.SelectedValue))
                dc.Add("CPHXC", ddlCPMCBH.SelectedValue);
            if (!string.IsNullOrEmpty(ddlCPXLBH.SelectedValue))
                dc.Add("CPXLBH", ddlCPXLBH.SelectedValue);
            if (!string.IsNullOrEmpty(ddlCPXHBH.SelectedValue.Trim()))
                dc.Add("CPXHBH", ddlCPXHBH.SelectedValue.Trim());
            using (ZL_CPBLL cVCPBLL = new ZL_CPBLL())
            {
                int RecordCount = 0;
                List<vCP> itemLists = cVCPBLL.GetCP(dc, "BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                this.GridView1.DataSource = itemLists;
                this.GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                vCP item=e.Row.DataItem as vCP;
                HyperLink hypl = e.Row.FindControl("hplyDetail") as HyperLink;
                if (!string.IsNullOrEmpty(hxcbh) && !string.IsNullOrEmpty(cpxbh))
                    hypl.NavigateUrl = "";
                else
                    hypl.NavigateUrl = string.Format("ZL_CP_Edit.aspx?id={0}", item.ID);
                hypl.Text = item.BH;
            }
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (ZL_CPBLL cBLL = new ZL_CPBLL())
                {
                    List<ZL_CP> items = new List<ZL_CP>();
                    ZL_CP item = new ZL_CP();
                    foreach (GridViewRow container in GridView1.Rows)
                    {
                        HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                        if (cBox != null && cBox.Checked)
                        {
                            item = cBLL.GetEntity(t => t.ID == new Guid(cBox.Value));
                            items.Add(item);
                        }
                    }
                    if (cBLL.Deletes(items))
                        ShowMessage("删除成功");
                }
            }
            catch (Exception ex)
            {
                this.ShowMessage("删除失败\n" + ex.Message);

            }
            finally
            {
                BindGridview();
            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (hxcbh != null && cpxbh != null)
                Response.Redirect(string.Format("ZL_CP_Add.aspx?wllxbh={0}&wlsxbh={1}", hxcbh, cpxbh));
            else
                Response.Redirect("ZL_CP_Add.aspx");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();
        }

        protected void ddlCPXBH_SelectedIndexChanged1(object sender, EventArgs e)
        {
            SelCPX();
        }

        private void SelCPX()
        {
            this.ddlCPXHBH.Items.Clear();
            if (!string.IsNullOrEmpty(this.ddlCPXBH.SelectedValue))
            {
                BindCPHXC(this.ddlCPMCBH, this.ddlCPXBH.SelectedValue, "请选择");
                BindCPXL(this.ddlCPXLBH, this.ddlCPXBH.SelectedValue, "请选择");
                BindCPXH(this.ddlCPXHBH, this.ddlCPXBH.SelectedValue, this.ddlCPMCBH.SelectedValue, "请选择");
            }
        }

        protected void ddlCPMCBH_SelectedIndexChanged1(object sender, EventArgs e)
        {
            this.ddlCPXHBH.Items.Clear();

            string cpx = this.ddlCPXBH.SelectedValue;
            string cpmc = this.ddlCPMCBH.SelectedValue;
            if (!string.IsNullOrEmpty(cpmc))
            {
                BindCPXH(this.ddlCPXHBH, this.ddlCPXBH.SelectedValue, this.ddlCPMCBH.SelectedValue, "请选择");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGridview();
        }
    }
}